package com.woniu.mall.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.woniu.common.entity.Result;
import com.woniu.mall.DTO.MallStoreDTO;
import com.woniu.mall.VO.ProductDetailVO;
import com.woniu.mall.VO.ProductListVO;
import com.woniu.mall.VO.StoreDetailVO;
import com.woniu.mall.VO.StoreListVO;
import com.woniu.mall.constant.MallPayInfoConstants;
import com.woniu.mall.mapper.MallStoreMapper;
import com.woniu.mall.pojo.MallStore;
import com.woniu.mall.service.IMallStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.pay.entity.PayInfo;
import com.woniu.pay.feign.PayFeignClient;
import com.woniu.user.feignClient.AddressFeignClient;
import com.woniu.user.feignClient.UserFeignClient;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 坤
 * @since 2023-05-22
 */
@Service
public class MallStoreServiceImpl extends ServiceImpl<MallStoreMapper, MallStore> implements IMallStoreService {
    @Resource
    private MallStoreMapper storeMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public StoreDetailVO showDetail(Integer sid) {
        StoreDetailVO storeDetailVO = storeMapper.showDetail(sid);
        return storeDetailVO;
    }

    @Override
    public List<StoreListVO> listSearch(String title) {
        List<StoreListVO> storeList = storeMapper.listSearch(title);
        return storeList;
    }

    @Override
    public Boolean saveStore(MallStoreDTO mallStoreDTO, UserLoginVO userLoginVO) {

        if (userLoginVO.getStoreId() != null && userLoginVO.getStoreId() != 0){
            return false;
        }

        storeMapper.saveStore(mallStoreDTO, userLoginVO);

        Integer id = storeMapper.selectIdByMobile(userLoginVO.getMobile());
        userFeignClient.updateStoreId(id, userLoginVO.getMobile());

        return true;
    }

    @Override
    public Boolean updateStore(MallStoreDTO mallStoreDTO, UserLoginVO userLoginVO) {

        Boolean flag = storeMapper.updateStore(mallStoreDTO, userLoginVO);
        return flag;
    }

    @Override
    public Boolean deleteStore(UserLoginVO userLoginVO) {
        Boolean flag = storeMapper.deleteStore(userLoginVO.getMobile());
        userFeignClient.updateStoreId(0,userLoginVO.getMobile());
        return flag;
    }
}
